<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>dbme</title>
<link rel="stylesheet" href="css/template.css" type="text/css" media="screen" title="no title" charset="utf-8" />

<style>
    table{
        width: 800px;
        margin-left: 10px;
        padding: 5px;
           
    }
  tr td {
       
        font-family: arial;
        border-bottom: 1px solid #000;
        border-right:  0px solid #000;
        text-align: left;
        margin-left: 10px;
        padding: 5px;
    }
    .prijs {
       
        font-family: arial;
        border-bottom: 1px solid #000;
        border-right:  0px solid #000;
        text-align: right;
        margin-left: 10px;
        padding: 5px;
    }
    .td{font-family: arial;
        border:  0px solid #000;
        text-align: left;
        margin-left: 10px;
        padding: 5px;
    }
    p {
    text-indent: 20px; 
    
    }

</style>
<script type="text/javascript">
function OnSubmitForm()
{
  if(document.pressed == 'Bereken mijn korting')
  {
   document.myform.action ="stap5.php";
  }
  if(document.pressed == 'offerte')
  {
   document.myform.action ="bestelling.php";
  }
 
  return true;
}
</script>
</head>
<table  widtd="800" collspan="0" cellspacing="0">  
<tbody>
   
   <?php  header('Content-type: text/html;  charset=utf-8', true);
        /*
         * To change tdis template, choose Tools | Templates
         * and open tde template in tde editor.
         */

session_start(); // Start session first thing in script/////////////////////////////////////
error_reporting(E_ALL);
include_once('controller/includes/db_access.php');

//die('datum'.$_POST['dt']);
// kijk of datum geselecteerd is vanaf stap1////////////////////////////////////
     if(isset($_POST['dt']) && ($_POST['dt'] == 'dt') ){           
    function dateformat($param) {
    // format "2012-06-19"  
    $dt = strftime("%Y-%m-%d", strtotime($param));
    // convertir les accents (pour encodage UTF-8)////////////////////////////////////////  
    $dt = mb_convert_encoding($dt, 'utf-8');
    return $dt;
    }

    //1 post datum from stap1
   if ($_POST['van']!='') $_SESSION['van'] = $_POST['van'];
   if ($_POST['tot']!='')$_SESSION['tot'] = $_POST['tot'];
    $van_datum = dateformat($_SESSION['van']);
    $tot_datum = dateformat($_SESSION['tot']);


    //1.1 count aantal dagen
    function daysDifference($endDate, $beginDate) 

    {
    //explode the date by "-" and storing to array
    $date_parts1 = explode("-", $beginDate);
    $date_parts2 = explode("-", $endDate);
    //gregoriantojd() Converts a Gregorian date to Julian Day Count
    $start_date = gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
    $end_date = gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
    return $end_date - $start_date;
    }


    $total_day = daysDifference($tot_datum, $van_datum);
    
    if($total_day <= 0) $total_day = 1;
    
    echo '<tr>
    <td widtd="100" class="td"></td><td colspan="3" class="td">Van: '.strftime("%d-%m-%Y", strtotime($van_datum)).'<br />T/m: '.strftime("%d-%m-%Y", strtotime($tot_datum)).'</td>
        </tr>';
    
     }  elseif (isset($_POST['dt']) && ($_POST['dt'] == 'gdt') ) {
         
         $van_datum = $_POST['van'];
         $tot_datum = $_POST['tot'];
         $total_day = $_POST['aantal_dag'];
          echo '<tr>
             
</td><td colspan="6" class="td">  
<p>'. date("d-m-y").'</p>
<p>Offerte voor '.$total_day.' dagen huur volgens onderstaande specificaties.</p>
<p><font color="red"> LET OP: </font>U heeft in uw aanvraag nog geen datum opgegeven dus u wordt
geadviseerd om de beschikbaarheid van deze materialen nog te verifiëren.</p>
<p>Neem alstublieft  contact op met Dbme via TEL. CONTACTNUMMER of
E-MAIL om deze offerte in een reservering om te zetten.</p>
</td>
        </tr>';

    }
   
    echo '<tr>
    <td widtd="50">Aantal</td>
    <td widtd="400">naam</td>
    <td widtd="75" class="prijs">Prijs/st</td>
    <td widtd="75" class="prijs">Dagprijs</td>
    <td widtd="100" class="prijs">Periode</td>
    <td widtd="80"class="td"></td>
    </tr>';
      if ($_POST['set_id']!='') $_SESSION['set_id'] = $_POST['set_id'];
    if (isset( $_SESSION['set_id'])>0) {
    $SQLSet = "SELECT * FROM sets WHERE set_id=" . $_SESSION['set_id'];
    $querySet = mysql_query($SQLSet);
    $rowSet = mysql_fetch_object($querySet);
    $setprijs =$rowSet->setprijs;
    $periode_setprijs =($setprijs * $total_day);
    //print_r($prodId_totalPerProdInSet);
    echo '<tr>
    <td>1</td>
    <td>'. $rowSet->naam.'</td>
    <td>&nbsp;</td>
    <td class="prijs">&euro; '.number_format($rowSet->setprijs, 2, ',', ' ').'</td>
    <td class="prijs">&euro; '.number_format($periode_setprijs, 2, ',', ' ').'</td>
   <td class="td"></td>
    </tr>';
    }


   if ($_POST['aantal']!='') $_SESSION["aantal"] = $_POST['aantal'];
    foreach ($_SESSION["aantal"] as $aantal_per_product) {

    $aantal .=  $aantal_per_product;
    $aantal_in_sys = array( $aantal,);
    // echo '<tr><td widtd="50%"></td><td widtd="25%"></td><td widtd="25%"></td></tr>';
    //echo $aantal_per_product;
    }


    $productteller=0;
   $_SESSION["aantalarray"]=$_SESSION["aantal"];
    $aantalarray= $_SESSION["aantalarray"];
    
    if ($_POST['product']!='') $_SESSION["product"]= $_POST['product'];
    
    foreach ($_SESSION["product"] as $product_id) {

    $SQLProd = "SELECT * FROM product WHERE product_id=" . $product_id;
    $queryProd = mysql_query($SQLProd);
    $rowPerProd = mysql_fetch_object($queryProd);
    $dagprijs= ($rowPerProd->huurprijs * $aantalarray[$productteller]);
    $all_periodePrijs = ($dagprijs * $total_day);
    $totaal_periodePrijs += $all_periodePrijs;
    $totaalhuurprijs +=$dagprijs;
    $naam .= $rowPerProd->naam;

    echo '<tr>
    <td>' . $aantalarray[$productteller] . '</td>
    <td>' . $rowPerProd->naam . '</td>
    <td class="prijs">&euro; ' .number_format($rowPerProd->huurprijs, 2, ',', ' ') . '</td>
    <td class="prijs">&euro; ' .number_format($dagprijs, 2, ',', ' ').'</td>
    <td class="prijs">&euro; ' .number_format($all_periodePrijs, 2, ',', ' ').'</td>
    <td class="td"></td>
    </tr>';
    $productteller++;
    }


    $dag_subtotaal =($totaalhuurprijs + $setprijs);
    $periode_subtotaal =($totaal_periodePrijs + $periode_setprijs);

    $staffelkorting_db=50; // UIT DB HALEN

    $staffelkorting = $staffelkorting_db/100; //0.50;

    $teBetalen = (1*$dag_subtotaal+(($total_day-1)*($dag_subtotaal*$staffelkorting)));

    $percentage = ($teBetalen/($dag_subtotaal*$total_day));

    $percentage = 100-($percentage*100);

    
    echo '<tr>
    <td class="td"></td>
    <td class="td"></td>
    <td>Subtotaal</td>
    <td class="prijs">&euro; '.number_format($dag_subtotaal, 2, ',', ' ').'</td>
    <td class="prijs">&euro; '.number_format($periode_subtotaal, 2, ',', '').'</td>
    <td class="td"></td>
    </tr>';
    
    
    echo '<tr>
    <td class="td"></td>
    <td class="td"></td>
    <td colspan="2">Staffelkorting ('.number_format($percentage,2, ',', ' ').'%)</td>
    <td class="prijs">-&nbsp;&nbsp; &euro; '.number_format(($periode_subtotaal - $teBetalen), 2, ',', ' ').'</td>
    <td class="td"></td>
    </tr>';
    
    
    echo '<tr>
    <td class="td">&nbsp;</td>
    <td class="td">&nbsp;</td>
    <td colspan="3">Subtotaal product</td>
    <td class="prijs">&euro; '.number_format($teBetalen, 2, ',', ' ').'</td>
    </tr>';
    
    
    echo '<tr>
    <td height="50" class="td" colspan="6"></td>
    </tr>';
    
    
    echo '<tr>
    <td widtd="100">aantal</td>
    <td widtd="400">naam</td>
    <td widtd="100" class="prijs">&nbsp;</td>
    <td widtd="100" class="prijs">Dagprijs</td>
    <td widtd="100" class="prijs">Periode</td>
    <td class="td"></td>
    </tr>';
    
    ///////BEGIN DIENST////////////////////////////////////////////////
    if($_POST['dienst'] != '') $_SESSION['dienst'] = $_POST['dienst'];
    if (isset($_SESSION['dienst'])) {
    foreach ($_SESSION['dienst'] as $dienst_id) {
    $sqlDienst= "SELECT * FROM diensten WHERE dienst_id=".$dienst_id;
    $resultDienst = mysql_query($sqlDienst);
    $rowDienst = mysql_fetch_object($resultDienst);
    $dienst_prijs=$rowDienst->prijs;
   //$totaal_periode_dienst+= $dienst_prijs;
    $totaal_periode_dienst_prijs += $dienst_prijs;

    if ($rowDienst->eenxdienst=='N'){
    $eenXdienst=$dienst_prijs;
    $totaaldienst_prijs +=$eenXdienst;
    $totaaldienst = $totaaldienst_prijs*$total_day;
    echo '<tr>
    <td>1</td>
    <td>'.$rowDienst->naam.'</td>
    <td>&nbsp;</td>
    <td class="prijs">&euro; '.number_format($dienst_prijs, 2, ',', ' ').'</td>
    <td class="prijs">&euro; '.number_format(($totaaldienst_prijs*$total_day), 2, ',', ' ').'</td>
    <td class="td"></td>
    </tr>';
    } else {
        
    echo '<tr>
    <td>1</td>
    <td>'.$rowDienst->naam.'</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td class="prijs">&euro; '.number_format($dienst_prijs, 2, ',', ' ').'</td>
    <td class="td"></td>
    </tr>';
    }
    
    }
    $exbtwbedrag = ($totaal_periode_dienst_prijs+$totaaldienst+$teBetalen);

    
//////////////////Subtotaal dienst(en)////////////////////////////////////
    echo '<tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td colspan="3"> Subtotaal dienst(en)</td>
    <td class="prijs">&euro; '.number_format(($totaal_periode_dienst_prijs+$totaaldienst), 2, ',', ' ').'</td>
    </tr>';
    }
    
    
    
     if(isset($_POST['kcode'])){
    
                 $kcode=$_POST['kcode'];   
                 
                 $sql_korting = mysql_query("SELECT * FROM cadeaubon WHERE code='$kcode' AND aktief='Y'");
                 $countkorting = mysql_num_rows($sql_korting);
                 $korting = mysql_fetch_array($sql_korting);
                 $type= $korting['type'];
                 $aktie = $korting['korting'];
                 
                  if ( $countkorting == 1){
                    $cadeaubon_id = $korting['cadeaubon'];      
                   switch($type){
                    case $type=='A':
                         $_SESSION["aktie_korting"] = $aktie;
                         $aktie = $_SESSION["aktie_korting"];
                         $korting_waarde = ($exbtwbedrag*($aktie / 100));
                         $_SESSION["aktie_waarde"]=$korting_waarde;
                         $korting_waarde = $_SESSION["aktie_waarde"];  
                  break;//end case $type=='A'
    
                  case $type=='C':
                        
                         $_SESSION["cadeaubon_waarde"] = $korting['korting'];
                         $cadeaubon = $_SESSION["cadeaubon_waarde"]; 
                         $waarde = $cadeaubon;

                 break; //end case $type=='C'
             

                 }//end switch $type=='C' or $type=='A'
                 
                 }
                 
                 }
                 if(isset($_SESSION["aktie_korting"])){
                     
                     $Subtotaal_exbtwbedrag =($exbtwbedrag - ($exbtwbedrag*($_SESSION["aktie_korting"] / 100)));

                 }else{
                     $Subtotaal_exbtwbedrag =$exbtwbedrag;
                     }
               $btwbedrag = ($Subtotaal_exbtwbedrag * 0.19);
               $inbtwbedrag =($Subtotaal_exbtwbedrag * 1.19);     
               if(isset($_SESSION["cadeaubon_waarde"])){
                     
                     $Total = ($inbtwbedrag - $_SESSION["cadeaubon_waarde"]);
                 }else{
                     $Total = $Subtotaal_exbtwbedrag+$btwbedrag;

                     }
  

                 
                 
                 
                 
    
    ///BEGIN SPACE////////////////////////////////////////////////////////
    echo '<tr>
    <td colspan="6" height="50" class=td></td>
    </tr>';
    
   ///BEGIN totaal excl BTW//////////////////////////////////////////////////////////////
   ////FORM kotingcode/////////////////////////////////////////////////////////////////////
    ?>
  
 <form name="myform" method="post" onsubmit="return OnSubmitForm();" > 
     
 <?php
  echo '<input type="hidden" name="van" value="'.$_POST['van'].'" />
       <input type="hidden" name="tot" value="'. $_POST['tot'].'" />
      <input type="hidden" name="cadeaubon" value="'.$cadeaubon_id.'" />

     <input type="hidden" name="dt" value="'. $_POST['dt'].'" />';
   if (isset( $_POST['set_id'])){
    echo'<input type="hidden" name="set_id" value="'.$_POST['set_id'].'" />';
   }
  if (isset( $_POST['product'])){
  foreach ($_POST["product"] as $product_id) {
  echo'<input type="hidden" name="product[]" value="'. $product_id. '" />';
   }
   }
 foreach ($_POST["aantal"] as $aantal) {
  echo'<input type="hidden" name="aantal[]" value="'. $aantal. '" /> ';
  }
  if (isset( $_POST['dienst'])){
  foreach ( $_POST['dienst'] as $dienst_id) {
  echo'<input type="hidden" name="dienst[]" value="'. $dienst_id. '" />';
  }
  
  }
   echo' <tr>
    <td class="td" colspan="2">';
if($_POST['kotingcode'] != 'TRUE')
    
    {
        echo '<input name="kotingcode" type="hidden" value="TRUE" /><input name="kcode" type="text" id="kcode"  value="" /><input type="submit"  onclick="document.pressed=this.value" value="Bereken mijn korting" /></td>';
    }
         echo' <td colspan="3">Totaal excl BTW</td>
    <td class="prijs">&euro;'.number_format($exbtwbedrag, 2, ',', ' ').'</td>
    <td class="td"></td>
    </tr>';

     if (isset($_SESSION["aktie_korting"]) > 0)
     
    {
     $korting_waarde = ($exbtwbedrag*($_SESSION["aktie_korting"] / 100));   
     echo '<tr>
    <td class="td">&nbsp;</td>
    <td class="td">&nbsp;</td>
    <td colspan="3">Korting ('.$_SESSION["aktie_korting"].'%)</td>
    <td class="prijs">-&nbsp;&nbsp; &euro; '.number_format($korting_waarde, 2, ',', ' ').'</td>
    <td class="td">&nbsp;</td>
    </tr>';
    }
            
    echo '<tr>
    <td class="td">&nbsp;</td>
    <td class="td">&nbsp;</td>
    <td colspan="3">BTW</td>
    <td class="prijs">&euro; '.number_format($btwbedrag, 2, ',', ' ').'</td>
    <td class="td">&nbsp;</td>
    </tr>';
    
    if (isset($_SESSION["cadeaubon_waarde"]) > 0){               
    echo'<tr>
    <td class="td">&nbsp;</td>
    <td class="td">&nbsp;</td>
    <td colspan="3">Korting incl BTW</td>
    <td class="prijs">-&nbsp;&nbsp; &euro;'.number_format($_SESSION["cadeaubon_waarde"], 2, ',', ' ').'</td>
    <td class="td">&nbsp;</td>
    </tr>'; 
    }

    echo '<tr>
    <td class="td">&nbsp;</td>
    <td class="td">&nbsp;</td>
    <td colspan="3">Totaal incl BTW</td>
    <td class="prijs">&euro; '.number_format($Total, 2, ',', ' ').'</td>
    </tr>';
    echo '<tr>
    <td height="50" class="td" colspan="6"></td>
    </tr>';
    
    echo '<tr>
    <td class="td" colspan="6">';
    
 if(isset ($_POST['aantal_dag'])&&( $_POST['aantal_dag'] > 0))
     
 {
     
     
if (isset($_POST['action']) && ($_POST['action'] == 'mail_factuur')) {
    
$body ='bestelling.php';
$subjet = 'Factuur van Dbme';
$email = $_POST['email'];

require 'controller/PHPMailer_5.2.1/class.phpmailer.php';
try {
   $mail = new PHPMailer(true); //New instance, with exceptions enabled
	
	//$body   = file_get_contents($body);
           
	$body             = preg_replace('/\\\\/','', $body); //Strip backslashes

	//$mail->IsSMTP();                           // tell the class to use SMTP
	//$mail->SMTPAuth   = true;                  // enable SMTP authentication
	//$mail->Port       = 25;                    // set the SMTP server port
	//$mail->Host       = "mail.yourdomain.com"; // SMTP server
	//$mail->Username   = "name@domain.com";     // SMTP server username
	//$mail->Password   = "password";            // SMTP server password

	//$mail->IsSendmail();  // tell the class to use Sendmail

	$mail->AddReplyTo("name@domain.com","First Last");

	$mail->From       = "info@dbme.nl";
	$mail->FromName   = "dbme.nl";

	$to = "$email";

	$mail->AddAddress($to);
        $mail->AddCC('Mamadou@thelimelight.nl', 'Mamadou');
	$mail->Subject  = $subjet;

	$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
	$mail->WordWrap   = 80; // set word wrap

	$mail->MsgHTML($body);

	$mail->IsHTML(true); // send as HTML

	$mail->Send();
        
        if ($mail->Send()) {
            
	echo 'Message has been sent.';
                
	} else {
            
	   // this else statement can be used to write into an error log if the mail function fails
	   // It can also be removed if you do not need error logging
	}
	
} catch (phpmailerException $e) {
	echo $e->errorMessage();
}
  }
     echo'<div>';
     //echo '<form name="mail_factuur" class="formular" method="post" action="stap5.php" >';
     echo '<input type="hidden" name="action" value="mail_factuur" />
     <input type="hidden" name="dt" value="'. $_POST['dt'].'" />
     <input type="hidden" name="aantal_dag" value="'. $_POST['aantal_dag'].'" />';
     echo '<p><input type="email" name="email" value="" size="40" placeholder="john@example.nl" /></p>';
     echo '<p><input type="submit" onclick="document.pressed=this.value" value="stuur naar mij deze factuur" /></p>';
     echo '</fieldset>';
     echo'</div>';
 }else{
     
  session_destroy();

?>
        
<input type="submit" name="nextstap" onclick="document.pressed=this.value" value="offerte" />
               
<?php } echo '</td></tr>';?>
          
</form>
    </tbody>
    </table>


</html>